import { defineStore } from "pinia";

import {
  productListRequest,
  productAddRequest,
  productUpdateRequest,
  productRemoveRequest,
} from "@/libs/service/api/house/index";
import { ref } from "vue";
import { IHouseForm } from '@/libs/type/house';

import { ElNotification } from "element-plus";

/**
 * 根
 */
export const housePinia = defineStore("house", () => {
  // state
  const list = ref<IHouseForm[]>([]);
  // actions
  async function listActions(playLoad: IHouseForm) {
    const result:any = await productListRequest(playLoad);
    list.value = result.data.list;
  }

  async function addActions(playLoad: IHouseForm) {
    const result = await productAddRequest(playLoad);
    if (result.code !== 200) {
      ElNotification({
        title: "失败",
        message: result.msg,
        type: "error",
      });
    }
    ElNotification({
      title: "成功",
      message: "提交成功",
      type: "success",
    });
    listActions({ current: 1,
      size: 10,});
  }

  async function updateActions(playLoad: IHouseForm) {
    const result = await productUpdateRequest(playLoad);
    if (result.code !== 200) {
      ElNotification({
        title: "失败",
        message: result.msg,
        type: "error",
      });
    }
    ElNotification({
      title: "成功",
      message: "提交成功",
      type: "success",
    });
    listActions({ current: 1,
      size: 10,});
  }

  async function removeActions(playLoad: IHouseForm) {
    const result = await productRemoveRequest(playLoad);
    if (result.code !== 200) {
      ElNotification({
        title: "失败",
        message: result.msg,
        type: "error",
      });
    }
    ElNotification({
      title: "成功",
      message: "删除成功",
      type: "success",
    });
    listActions({ current: 1,
      size: 10,});
  }

  return {
    list,
    listActions,
    addActions,
    updateActions,
    removeActions,
  };
});
